package comp512.classes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

public final class DB {
	private static DB instance = null;
	private HashMap<String, Question> questionDB; 
	
	protected DB() {
		questionDB = new HashMap<>();
	}

	public synchronized static DB getInstance() {
		if (instance == null) {
			instance = new DB();
		}
		return instance;
	}
	
	public boolean addQuestion(Question q){
		if(!questionDB.containsKey(q.getQuestion()))
		{
			questionDB.put(q.getQuestion(), q);
			return true;
		}
		
		return false;
	}
	
	public Question getRandomQuestion(){
		Random rand = new Random();
		Object[] entries = questionDB.entrySet().toArray();
		Object randVal = entries[rand.nextInt(entries.length)];
		return questionDB.get(randVal);
	}
	
	public Question getQuestion(int qPosition){
		List<String> keys = new ArrayList<String>(questionDB.keySet());
		String qKey = keys.get(qPosition);
		Question rtn = questionDB.get(qKey);		
		return rtn;
	}
	
	public int getQuestionCount(){
		return questionDB.size();
	}
	
}
